package com.atguigu.daijia.order.controller;

import com.atguigu.daijia.common.result.Result;
import com.atguigu.daijia.model.entity.order.OrderInfo;
import com.atguigu.daijia.model.form.order.OrderInfoForm;
import com.atguigu.daijia.model.form.order.StartDriveForm;
import com.atguigu.daijia.model.form.order.UpdateOrderBillForm;
import com.atguigu.daijia.model.form.order.UpdateOrderCartForm;
import com.atguigu.daijia.model.vo.base.PageVo;
import com.atguigu.daijia.model.vo.order.*;
import com.atguigu.daijia.order.service.OrderInfoService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;


@Tag(name = "订单API接口管理")
@RestController
@RequestMapping(value="/order/info")
@SuppressWarnings({"unchecked", "rawtypes"})
@RequiredArgsConstructor
public class OrderInfoController {

    private final OrderInfoService orderInfoService;

    /**
     * 保存订单信息
     * @param orderInfoForm
     * @return
     */
    @Operation(summary = "保存订单信息")
    @PostMapping("/saveOrderInfo")
    public Result<Long> saveOrderInfo(@RequestBody OrderInfoForm orderInfoForm) {
        Long orderId = orderInfoService.saveOrderInfo(orderInfoForm);
        return Result.ok(orderId);
    }

    /**
     * 根据订单 ID 查询订单状态
     * @param orderId
     * @return
     */
    @Operation(summary = "根据订单 ID 查询订单状态")
    @GetMapping("/getOrderStatus/{orderId}")
    public Result<Integer> getOrderStatus(@PathVariable("orderId") Long orderId) {
        Integer status = orderInfoService.getOrderStatus(orderId);
        return Result.ok(status);
    }

    /**
     * 司机抢单
     * @param driverId
     * @param orderId
     * @return
     */
    @Operation(summary = "司机抢单")
    @GetMapping("/robNewOrder/{driverId}/{orderId}")
    public Result<Boolean> robNewOrder(@PathVariable("driverId") Long driverId, @PathVariable("orderId") Long orderId) {
        Boolean result = orderInfoService.robNewOrder(driverId, orderId);
        return Result.ok(result);
    }

    /**
     * 查询乘客执行中订单基本信息
     * @param customerId
     * @return
     */
    @Operation(summary = "查询乘客执行中订单基本信息")
    @GetMapping("/searchCustomerCurrentOrder/{customerId}")
    public Result<CurrentOrderInfoVo> searchCustomerCurrentOrder(@PathVariable("customerId") Long customerId) {
        CurrentOrderInfoVo currentOrderInfoVo = orderInfoService.searchCustomerCurrentOrder(customerId);
        return Result.ok(currentOrderInfoVo);
    }

    /**
     * 查询司机执行中订单基本信息
     * @param driverId
     * @return
     */
    @Operation(summary = "查询司机执行中订单基本信息")
    @GetMapping("/searchDriverCurrentOrder/{driverId}")
    public Result<CurrentOrderInfoVo> searchDriverCurrentOrder(@PathVariable("driverId") Long driverId) {
        CurrentOrderInfoVo currentOrderInfoVo = orderInfoService.searchDriverCurrentOrder(driverId);
        return Result.ok(currentOrderInfoVo);
    }

    /**
     * 根据订单 ID 查询订单详情
     * @param orderId
     * @return
     */
    @Operation(summary = "根据订单 ID 查询订单信息")
    @GetMapping("/getOrderInfo/{orderId}")
    public Result<OrderInfo> getOrderInfo(@PathVariable("orderId") Long orderId) {
        OrderInfo orderInfo = orderInfoService.getById(orderId);
        return Result.ok(orderInfo);
    }

    /**
     * 接单司机到达代驾订单起始点
     * @param orderId
     * @param driverId
     * @return
     */
    @Operation(summary = "接单司机到达代驾订单起始点")
    @GetMapping("/driverArriveStartLocation/{orderId}/{driverId}")
    public Result<Boolean> driverArriveStartLocation(@PathVariable("orderId") Long orderId, @PathVariable("driverId") Long driverId) {
        Boolean result = orderInfoService.driverArriveStartLocation(orderId, driverId);
        return Result.ok(result);
    }

    /**
     * 更新代驾车辆信息
     * @param updateOrderCartForm
     * @return
     */
    @Operation(summary = "更新代驾车辆信息")
    @PostMapping("/updateOrderCart")
    public Result<Boolean> updateOrderCar(@RequestBody UpdateOrderCartForm updateOrderCartForm) {
        Boolean result = orderInfoService.updateOrderCar(updateOrderCartForm);
        return Result.ok(result);
    }

    /**
     * 开始代驾服务
     * @param startDriveForm
     * @return
     */
    @Operation(summary = "开始代驾服务")
    @PostMapping("/startDrive")
    public Result<Boolean> startDrive(@RequestBody StartDriveForm startDriveForm) {
        Boolean result = orderInfoService.startDrive(startDriveForm);
        return Result.ok(result);
    }

    /**
     * 根据时间获取订单数量
     * @param startTime
     * @param endTime
     * @return
     */
    @Operation(summary = "根据时间获取订单数量")
    @GetMapping("/getOrderNumByTime/{startTime}/{endTime}/{driverId}")
    public Result<Long> getOrderNumByTime(@PathVariable("startTime") String startTime, @PathVariable("endTime") String endTime, @PathVariable("driverId") Long driverId) {
        Long num = orderInfoService.getOrderNumByTime(startTime, endTime, driverId);
        return Result.ok(num);
    }

    /**
     * 结束代驾服务更新数据库表信息
     * @param updateOrderBillForm
     * @return
     */
    @Operation(summary = "结束代驾服务更新数据库表信息")
    @PostMapping("/endDrive")
    public Result<Boolean> endDrive(@RequestBody UpdateOrderBillForm updateOrderBillForm) {
        Boolean result = orderInfoService.endDrive(updateOrderBillForm);
        return Result.ok(result);
    }

    /**
     * 分页查询乘客订单列表
     * @param customerId
     * @param page
     * @param limit
     * @return
     */
    @Operation(summary = "分页查询乘客订单列表")
    @GetMapping("/findCustomerOrderPage/{customerId}/{page}/{limit}")
    public Result<PageVo> findCustomerOrderPage(
            @Parameter(name = "customerId", description = "乘客 ID", required = true) @PathVariable("customerId") Long customerId,
            @Parameter(name = "page", description = "当前页码", required = true) @PathVariable("page") Long page,
            @Parameter(name = "limit", description = "每页记录数", required = true) @PathVariable("limit") Long limit) {
        Page<OrderInfo> pageParam = new Page<>(page, limit);
        PageVo pageVo = orderInfoService.findCustomerOrderPage(pageParam, customerId);
        pageVo.setPage(page);
        pageVo.setLimit(limit);
        return Result.ok(pageVo);
    }

    /**
     * 分页查询司机订单列表
     * @param driverId
     * @param page
     * @param limit
     * @return
     */
    @Operation(summary = "分页查询司机订单列表")
    @GetMapping("/findDriverOrderPage/{driverId}/{page}/{limit}")
    public Result<PageVo> findDriverOrderPage(@PathVariable("driverId") Long driverId, @PathVariable("page") Long page, @PathVariable("limit") Long limit) {
        Page<OrderInfo> pageParam = new Page<>(page, limit);
        PageVo pageVo = orderInfoService.findDriverOrderPage(pageParam, driverId);
        pageVo.setPage(page);
        pageVo.setLimit(limit);
        return Result.ok(pageVo);
    }

    /**
     * 根据订单 ID 查询订单账单信息
     * @param orderId
     * @return
     */
    @Operation(summary = "根据订单 ID 查询订单账单信息")
    @GetMapping("/getOrderBillInfo/{orderId}")
    public Result<OrderBillVo> getOrderBillInfo(@PathVariable("orderId") Long orderId) {
        OrderBillVo orderBillVo = orderInfoService.getOrderBillInfo(orderId);
        return Result.ok(orderBillVo);
    }

    /**
     * 根据订单 ID 查询订单分账信息
     * @param orderId
     * @return
     */
    @Operation(summary = "根据订单 ID 查询订单分账信息")
    @GetMapping("/getOrderProfitsharing/{orderId}")
    public Result<OrderProfitsharingVo> getOrderProfitsharing(@PathVariable("orderId") Long orderId) {
        OrderProfitsharingVo orderProfitsharingVo = orderInfoService.getOrderProfitsharing(orderId);
        return Result.ok(orderProfitsharingVo);
    }

    /**
     * 发送订单的账单信息给乘客
     * @param orderId
     * @param driverId
     * @return
     */
    @Operation(summary = "发送订单的账单信息给乘客")
    @GetMapping("/sendOrderBillInfo/{orderId}/{driverId}")
    public Result<Boolean> sendOrderBillInfo(@PathVariable("orderId") Long orderId, @PathVariable("driverId") Long driverId) {
        Boolean result = orderInfoService.sendOrderBillInfo(orderId, driverId);
        return Result.ok(result);
    }

    /**
     * 查询订单支付信息
     * @param orderNo
     * @param customerId
     * @return
     */
    @Operation(summary = "查询订单支付信息")
    @GetMapping("/getOrderPayVo/{orderNo}/{customerId}")
    public Result<OrderPayVo> getOrderPayVo(@PathVariable("orderNo") String orderNo, @PathVariable("customerId") Long customerId) {
        OrderPayVo orderPayVo = orderInfoService.getOrderPayVo(orderNo, customerId);
        return Result.ok(orderPayVo);
    }

    /**
     * 更新订单支付状态
     * @param orderNo
     * @return
     */
    @Operation(summary = "更新订单支付状态")
    @GetMapping("/updateOrderPayStatus/{orderNo}")
    public Result<Boolean> updateOrderPayStatus(@PathVariable("orderNo") String orderNo) {
        Boolean result = orderInfoService.updateOrderPayStatus(orderNo);
        return Result.ok(result);
    }

    /**
     * 获取订单的系统奖励
     * @param orderNo
     * @return
     */
    @Operation(summary = "获取订单的系统奖励")
    @GetMapping("/getOrderRewardFee/{orderNo}")
    public Result<OrderRewardVo> getOrderRewardFee(@PathVariable("orderNo") String orderNo) {
        OrderRewardVo orderRewardVo = orderInfoService.getOrderRewardFee(orderNo);
        return Result.ok(orderRewardVo);
    }

    /**
     * 更新订单优惠券金额
     * @param orderId
     * @param couponAmount
     * @return
     */
    @Operation(summary = "更新订单优惠券金额")
    @GetMapping("/updateCouponAmount/{orderId}/{couponAmount}")
    public Result<Boolean> updateOrderCouponAmount(@PathVariable("orderId") Long orderId, @PathVariable("couponAmount") BigDecimal couponAmount) {
        Boolean result = orderInfoService.updateOrderCouponAmount(orderId, couponAmount);
        return Result.ok(result);
    }

}

